﻿using ChoMy.Helpers;
using ChoMy.Models;
using ChoMy.Models.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChoMy.Controllers
{
    public class LayoutController : Controller
    {
        private ChoMyContext db = new ChoMyContext();

        public ActionResult _MenuOne(string search)
        {
            List<Category> categories = db.Categories
                .Where(x => x.Level < 3
                    && !x.IsDelete)
                .ToList();

            ViewBag.Search = search;

            return PartialView(categories);
        }

        public ActionResult _MenuTwo(int? id)
        {
            if(id == null)
            {
                return null;
            }

            Category category = db.Categories
                .FirstOrDefault(x => x.Id == id
                    && !x.IsDelete);

            if(category == null)
            {
                return null;
            }

            ViewBag.Category = category;

            List<Category> categories = db.Categories
                .Where(x => x.ParentId == id
                    && !x.IsDelete)
                .ToList();

            return PartialView(categories);
        }

        public ActionResult _PostInFooter(string groupNameId)
        {
            PostGroup postGroup = db.PostGroups
                .FirstOrDefault(x => x.NameId == groupNameId
                    && !x.IsDelete);

            if (postGroup != null)
            {
                ViewBag.PostGroup = postGroup;

                List<Post> posts = postGroup.PostInGroups
                    .Select(x => x.Post)
                    .Where(x => !x.IsDelete)
                    .ToList();

                return PartialView(posts);
            }

            return null;
        }

        public ActionResult _PostInCopyright(string groupNameId)
        {
            PostGroup postGroup = db.PostGroups
                .FirstOrDefault(x => x.NameId == groupNameId
                    && !x.IsDelete);

            if (postGroup != null)
            {
                ViewBag.PostGroup = postGroup;

                List<Post> posts = postGroup.PostInGroups
                    .Select(x => x.Post)
                    .Where(x => !x.IsDelete)
                    .ToList();

                return PartialView(posts);
            }

            return null;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }

            base.Dispose(disposing);
        }
    }
}